package facade;

import java.io.*;

public class TextFileWriterFacade {

	private String filepath;
	private FileWriter opened_file;
	private BufferedWriter opened_buffer;
	
	public TextFileWriterFacade()
	{
		filepath = null;
		opened_file = null;
		opened_buffer = null;
	}
	
	public void open_file(String filepath)
	{
		this.filepath = filepath;
		System.out.println("\t[FACADE] Opening file: " + filepath);
		try {
			opened_file = new FileWriter(filepath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("\t[FACADE] Creating a BufferedWriter for opened file: " + filepath);
		opened_buffer = new BufferedWriter(opened_file);
	}
	
	public void write_text_to_file(String txt)
	{
		try {
			System.out.println("\t[FACADE] Writing [" + txt + "] to file: " + filepath);
			opened_buffer.write(txt);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void write_multiple_empty_lines_to_file(int nb_lines)
	{
		try {		
			for(int i = 0 ; i < nb_lines ; i++)
			{
				opened_buffer.newLine();
			}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void write_title_to_file(String title)
	{
		try {
			opened_buffer.write("******************************************\n");
			opened_buffer.write("*\t" + title+"\t*\n");
			opened_buffer.write("******************************************\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void write_comment_to_file(String comment)
	{
		try {
			opened_buffer.write("#\tComment\n");
			opened_buffer.write("#\t" + comment+"\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void close_file()
	{
		try {
			System.out.println("\t[FACADE] Closing BufferedWriter for opened file: " + filepath);
			opened_buffer.close();
			System.out.println("\t[FACADE] Closing file: " + filepath);
			opened_file.close();
			filepath = null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
